<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>nih: sfmtrand/sfmtrand.h Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">nih
   &#160;<span id="projectnumber">0.5</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('sfmtrand_8h.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">sfmtrand/sfmtrand.h</div>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">//</span>
<a name="l00002"></a>00002 <span class="comment">// C++ class template imprementation of SFMT(SSE2)</span>
<a name="l00003"></a>00003 <span class="comment">//</span>
<a name="l00004"></a>00004 <span class="comment">// Copyright (c) 2008 Tripcode Explorer Project. Rights reserved.</span>
<a name="l00005"></a>00005 <span class="comment">//</span>
<a name="l00006"></a>00006 <span class="comment">// Redistribution and use in source and binary forms, with or without</span>
<a name="l00007"></a>00007 <span class="comment">// modification, are permitted provided that the following conditions are</span>
<a name="l00008"></a>00008 <span class="comment">// met:</span>
<a name="l00009"></a>00009 <span class="comment">//</span>
<a name="l00010"></a>00010 <span class="comment">//    * Redistributions of source code must retain the above copyright</span>
<a name="l00011"></a>00011 <span class="comment">//      notice, this list of conditions and the following disclaimer.</span>
<a name="l00012"></a>00012 <span class="comment">//    * Redistributions in binary form must reproduce the above</span>
<a name="l00013"></a>00013 <span class="comment">//      copyright notice, this list of conditions and the following</span>
<a name="l00014"></a>00014 <span class="comment">//      disclaimer in the documentation and/or other materials provided</span>
<a name="l00015"></a>00015 <span class="comment">//      with the distribution.</span>
<a name="l00016"></a>00016 <span class="comment">//</span>
<a name="l00017"></a>00017 <span class="comment">// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span>
<a name="l00018"></a>00018 <span class="comment">// &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span>
<a name="l00019"></a>00019 <span class="comment">// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR</span>
<a name="l00020"></a>00020 <span class="comment">// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT</span>
<a name="l00021"></a>00021 <span class="comment">// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<a name="l00022"></a>00022 <span class="comment">// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span>
<a name="l00023"></a>00023 <span class="comment">// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,</span>
<a name="l00024"></a>00024 <span class="comment">// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY</span>
<a name="l00025"></a>00025 <span class="comment">// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
<a name="l00026"></a>00026 <span class="comment">// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span>
<a name="l00027"></a>00027 <span class="comment">// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<a name="l00028"></a>00028 
<a name="l00029"></a>00029 <span class="comment">// Original imprementation of SFMT (SIMD-oriented Fast Mersenne Twister)</span>
<a name="l00030"></a>00030 <span class="comment">//   Copyright (C) 2006, 2007 Mutsuo Saito, Makoto Matsumoto and</span>
<a name="l00031"></a>00031 <span class="comment">//   Hiroshima University. All rights reserved.</span>
<a name="l00032"></a>00032 <span class="comment">//   (see original/LICENSE.txt)</span>
<a name="l00033"></a>00033 
<a name="l00034"></a>00034 <span class="preprocessor">#ifndef SSE2_SFMT_PLUS__</span>
<a name="l00035"></a>00035 <span class="preprocessor"></span><span class="preprocessor">#define SSE2_SFMT_PLUS__</span>
<a name="l00036"></a>00036 <span class="preprocessor"></span>
<a name="l00037"></a>00037 <span class="keyword">namespace </span>sfmtrand {
<a name="l00038"></a>00038 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> copyright[] = <span class="stringliteral">&quot;SFMT implementation Copyright (c) 2008 Tripcode Explorer Project. Rights reserved.\n&quot;</span>;
<a name="l00039"></a>00039 }
<a name="l00040"></a>00040 
<a name="l00041"></a>00041 <span class="preprocessor">#include &lt;emmintrin.h&gt;</span>
<a name="l00042"></a>00042 <span class="preprocessor">#include &lt;cstdio&gt;</span>
<a name="l00043"></a>00043 <span class="preprocessor">#include &lt;cassert&gt;</span>
<a name="l00044"></a>00044 <span class="preprocessor">#if defined(__STDC_VERSION__) &amp;&amp; (__STDC_VERSION__ &gt;= 199901L)</span>
<a name="l00045"></a>00045 <span class="preprocessor"></span><span class="preprocessor">  #include &lt;inttypes.h&gt;</span>
<a name="l00046"></a>00046 <span class="preprocessor">#elif defined(_MSC_VER) || defined(__BORLANDC__)</span>
<a name="l00047"></a>00047 <span class="preprocessor"></span>  <span class="keyword">typedef</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> uint32_t;
<a name="l00048"></a>00048   <span class="keyword">typedef</span> <span class="keywordtype">unsigned</span> __int64 uint64_t;
<a name="l00049"></a>00049 <span class="preprocessor">  #define forceinline __inline</span>
<a name="l00050"></a>00050 <span class="preprocessor"></span><span class="preprocessor">#else</span>
<a name="l00051"></a>00051 <span class="preprocessor"></span><span class="preprocessor">  #include &lt;inttypes.h&gt;</span>
<a name="l00052"></a>00052 <span class="preprocessor">  #if defined(__GNUC__)</span>
<a name="l00053"></a>00053 <span class="preprocessor"></span><span class="preprocessor">    #define forceinline __inline__</span>
<a name="l00054"></a>00054 <span class="preprocessor"></span><span class="preprocessor">  #endif</span>
<a name="l00055"></a>00055 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00056"></a>00056 <span class="preprocessor"></span>
<a name="l00057"></a>00057 <span class="keyword">namespace </span>sfmtplus { 
<a name="l00058"></a>00058         <span class="keyword">template</span> &lt; uint32_t mexp, uint32_t pos1, 
<a name="l00059"></a>00059                 uint32_t sl1, uint32_t sl2, uint32_t sr1, uint32_t sr2, 
<a name="l00060"></a>00060                 uint32_t msk1, uint32_t msk2, uint32_t msk3, uint32_t msk4,
<a name="l00061"></a>00061                 uint32_t parity1, uint32_t parity2, uint32_t parity3, uint32_t parity4 &gt;
<a name="l00062"></a><a class="code" href="classsfmtplus_1_1sse2__fast__mersenne__twister.html">00062</a>         <span class="keyword">class </span><a class="code" href="classsfmtplus_1_1sse2__fast__mersenne__twister.html">sse2_fast_mersenne_twister</a> {
<a name="l00063"></a>00063         <span class="keyword">public</span>:
<a name="l00064"></a>00064                 <span class="keyword">enum</span> { 
<a name="l00065"></a>00065                         MEXP = mexp,
<a name="l00066"></a>00066                         POS1 = pos1, 
<a name="l00067"></a>00067                         SL1 = sl1, 
<a name="l00068"></a>00068                         SL2 = sl2,
<a name="l00069"></a>00069                         SR1 = sr1,
<a name="l00070"></a>00070                         SR2 = sr2, 
<a name="l00071"></a>00071                         MSK1 = msk1,
<a name="l00072"></a>00072                         MSK2 = msk2,
<a name="l00073"></a>00073                         MSK3 = msk3,
<a name="l00074"></a>00074                         MSK4 = msk4,
<a name="l00075"></a>00075                         PARITY1 = parity1, 
<a name="l00076"></a>00076                         PARITY2 = parity2, 
<a name="l00077"></a>00077                         PARITY3 = parity3, 
<a name="l00078"></a>00078                         PARITY4 = parity4,
<a name="l00079"></a>00079                         N = MEXP / 128 + 1,
<a name="l00080"></a>00080                         N32 = N * 4,
<a name="l00081"></a>00081                         N64 = N * 2
<a name="l00082"></a>00082                 };
<a name="l00083"></a>00083 
<a name="l00084"></a>00084         <span class="keyword">protected</span>:
<a name="l00085"></a>00085                 forceinline <span class="keyword">static</span> <span class="keywordtype">double</span> to_real2(uint32_t v) { <span class="keywordflow">return</span> v * (1.0/4294967296.0); }
<a name="l00086"></a>00086                 forceinline <span class="keyword">static</span> <span class="keywordtype">double</span> to_real1(uint32_t v) { <span class="keywordflow">return</span> v * (1.0/4294967295.0); }
<a name="l00087"></a>00087                 forceinline <span class="keyword">static</span> <span class="keywordtype">double</span> to_real3(uint32_t v) { <span class="keywordflow">return</span> (((<span class="keywordtype">double</span>)v) + 0.5)*(1.0/4294967296.0); }
<a name="l00088"></a>00088                 forceinline <span class="keyword">static</span> <span class="keywordtype">double</span> to_res53(uint64_t v) { <span class="keywordflow">return</span> v * (1.0/18446744073709551616.0L); };
<a name="l00089"></a>00089                 forceinline <span class="keyword">static</span> <span class="keywordtype">double</span> to_res53_mix(uint32_t x, uint32_t y) { <span class="keywordflow">return</span> to_res53(x | ((uint64_t)y &lt;&lt; 32)); }
<a name="l00090"></a>00090                 forceinline <span class="keyword">static</span> <span class="keywordtype">int</span> idxof(<span class="keywordtype">int</span> i) { <span class="keywordflow">return</span> i; }
<a name="l00091"></a>00091                 forceinline <span class="keyword">static</span> uint32_t func1(uint32_t x) { <span class="keywordflow">return</span> (x ^ (x &gt;&gt; 27)) * (uint32_t)1664525UL; }
<a name="l00092"></a>00092                 forceinline <span class="keyword">static</span> uint32_t func2(uint32_t x) { <span class="keywordflow">return</span> (x ^ (x &gt;&gt; 27)) * (uint32_t)1566083941UL; }
<a name="l00093"></a>00093 
<a name="l00094"></a><a class="code" href="unionsfmtplus_1_1sse2__fast__mersenne__twister_1_1w128__t.html">00094</a>                 <span class="keyword">union </span><a class="code" href="unionsfmtplus_1_1sse2__fast__mersenne__twister_1_1w128__t.html">w128_t</a> {
<a name="l00095"></a>00095                         __m128i si;
<a name="l00096"></a>00096                         uint32_t u[4];
<a name="l00097"></a>00097                 };
<a name="l00098"></a>00098 
<a name="l00099"></a>00099                 <a class="code" href="unionsfmtplus_1_1sse2__fast__mersenne__twister_1_1w128__t.html">w128_t</a> sfmt[N];
<a name="l00100"></a>00100                 uint32_t *psfmt32;
<a name="l00101"></a>00101                 uint64_t *psfmt64;
<a name="l00102"></a>00102                 <span class="keywordtype">int</span> idx;
<a name="l00103"></a>00103                 <span class="keywordtype">int</span> initialized;
<a name="l00104"></a>00104                 uint32_t parity[4];
<a name="l00105"></a>00105                 <span class="keywordtype">char</span> idstring[80];
<a name="l00106"></a>00106 
<a name="l00107"></a>00107                 <span class="keyword">static</span> __m128i mm_recursion(__m128i *a, <span class="keyword">const</span> __m128i* b, __m128i c,
<a name="l00108"></a>00108                         __m128i d, __m128i mask) {
<a name="l00109"></a>00109                         __m128i v, x, y, z;
<a name="l00110"></a>00110                         x = _mm_load_si128(a);
<a name="l00111"></a>00111                         y = _mm_srli_epi32(*b, SR1);
<a name="l00112"></a>00112                         z = _mm_srli_si128(c, SR2);
<a name="l00113"></a>00113                         v = _mm_slli_epi32(d, SL1);
<a name="l00114"></a>00114                         z = _mm_xor_si128(z, x);
<a name="l00115"></a>00115                         z = _mm_xor_si128(z, v);
<a name="l00116"></a>00116                         x = _mm_slli_si128(x, SL2);
<a name="l00117"></a>00117                         y = _mm_and_si128(y, mask);
<a name="l00118"></a>00118                         z = _mm_xor_si128(z, x);
<a name="l00119"></a>00119                         z = _mm_xor_si128(z, y);
<a name="l00120"></a>00120                         <span class="keywordflow">return</span> z;
<a name="l00121"></a>00121                 }
<a name="l00122"></a>00122                 <span class="keywordtype">void</span> period_certification(<span class="keywordtype">void</span>) {
<a name="l00123"></a>00123                         <span class="keywordtype">int</span> inner = 0;
<a name="l00124"></a>00124                         <span class="keywordtype">int</span> i, j;
<a name="l00125"></a>00125                         uint32_t work;
<a name="l00126"></a>00126 
<a name="l00127"></a>00127                         <span class="keywordflow">for</span> (i = 0; i &lt; 4; i++)
<a name="l00128"></a>00128                                 inner ^= psfmt32[idxof(i)] &amp; parity[i];
<a name="l00129"></a>00129                         <span class="keywordflow">for</span> (i = 16; i &gt; 0; i &gt;&gt;= 1)
<a name="l00130"></a>00130                                 inner ^= inner &gt;&gt; i;
<a name="l00131"></a>00131                         inner &amp;= 1;
<a name="l00132"></a>00132                         <span class="comment">/* check OK */</span>
<a name="l00133"></a>00133                         <span class="keywordflow">if</span> (inner == 1) {
<a name="l00134"></a>00134                                 <span class="keywordflow">return</span>;
<a name="l00135"></a>00135                         }
<a name="l00136"></a>00136                         <span class="comment">/* check NG, and modification */</span>
<a name="l00137"></a>00137                         <span class="keywordflow">for</span> (i = 0; i &lt; 4; i++) {
<a name="l00138"></a>00138                                 work = 1;
<a name="l00139"></a>00139                                 <span class="keywordflow">for</span> (j = 0; j &lt; 32; j++) {
<a name="l00140"></a>00140                                         <span class="keywordflow">if</span> ((work &amp; parity[i]) != 0) {
<a name="l00141"></a>00141                                                 psfmt32[idxof(i)] ^= work;
<a name="l00142"></a>00142                                                 <span class="keywordflow">return</span>;
<a name="l00143"></a>00143                                         }
<a name="l00144"></a>00144                                         work = work &lt;&lt; 1;
<a name="l00145"></a>00145                                 }
<a name="l00146"></a>00146                         }
<a name="l00147"></a>00147                 }
<a name="l00148"></a>00148         <span class="keyword">public</span>:
<a name="l00149"></a>00149                 sse2_fast_mersenne_twister(<span class="keywordtype">void</span>) {
<a name="l00150"></a>00150                         psfmt32 = &amp;sfmt[0].u[0];
<a name="l00151"></a>00151                         psfmt64 = (uint64_t *)&amp;sfmt[0].u[0];
<a name="l00152"></a>00152                         initialized = 0;
<a name="l00153"></a>00153                         parity[0] = PARITY1; 
<a name="l00154"></a>00154                         parity[1] = PARITY2;
<a name="l00155"></a>00155                         parity[2] = PARITY3;
<a name="l00156"></a>00156                         parity[3] = PARITY4;
<a name="l00157"></a>00157                         std::sprintf(idstring, <span class="stringliteral">&quot;SFMT-%d:%d-%d-%d-%d-%d:%08x-%08x-%08x-%08x&quot;</span>,
<a name="l00158"></a>00158                                 MEXP, POS1, SL1, SL2, SR1, SR2, MSK1, MSK2, MSK3, MSK4);
<a name="l00159"></a>00159                 }
<a name="l00160"></a>00160                 sse2_fast_mersenne_twister(uint32_t seed) {
<a name="l00161"></a>00161                         sse2_fast_mersenne_twister();
<a name="l00162"></a>00162                         init_gen_rand(seed);
<a name="l00163"></a>00163                 }
<a name="l00164"></a>00164                 sse2_fast_mersenne_twister(uint32_t *init_key, <span class="keywordtype">int</span> key_length) {
<a name="l00165"></a>00165                         sse2_fast_mersenne_twister();
<a name="l00166"></a>00166                         init_by_array(init_key, key_length);
<a name="l00167"></a>00167                 }
<a name="l00168"></a>00168                 <span class="keywordtype">void</span> gen_rand_all(<span class="keywordtype">void</span>) {
<a name="l00169"></a>00169                         <span class="keywordtype">int</span> i;
<a name="l00170"></a>00170                         __m128i r, r1, r2;
<a name="l00171"></a>00171                         <span class="comment">/*static*/</span> <span class="keyword">const</span> __m128i mask = _mm_set_epi32( MSK4, MSK3, MSK2, MSK1 );
<a name="l00172"></a>00172 
<a name="l00173"></a>00173                         r1 = _mm_load_si128(&amp;sfmt[N - 2].si);
<a name="l00174"></a>00174                         r2 = _mm_load_si128(&amp;sfmt[N - 1].si);
<a name="l00175"></a>00175                         <span class="keywordflow">for</span> (i = 0; i &lt; N - POS1; i++) {
<a name="l00176"></a>00176                                 r = mm_recursion(&amp;sfmt[i].si, &amp;sfmt[i + POS1].si, r1, r2, mask);
<a name="l00177"></a>00177                                 _mm_store_si128(&amp;sfmt[i].si, r);
<a name="l00178"></a>00178                                 r1 = r2;
<a name="l00179"></a>00179                                 r2 = r;
<a name="l00180"></a>00180                         }
<a name="l00181"></a>00181                         <span class="keywordflow">for</span> (; i &lt; N; i++) {
<a name="l00182"></a>00182                                 r = mm_recursion(&amp;sfmt[i].si, &amp;sfmt[i + POS1 - N].si, r1, r2, mask);
<a name="l00183"></a>00183                                 _mm_store_si128(&amp;sfmt[i].si, r);
<a name="l00184"></a>00184                                 r1 = r2;
<a name="l00185"></a>00185                                 r2 = r;
<a name="l00186"></a>00186                         }
<a name="l00187"></a>00187                 }
<a name="l00188"></a>00188                 <span class="keywordtype">void</span> gen_rand_array(w128_t *array, <span class="keywordtype">int</span> size) {
<a name="l00189"></a>00189                         <span class="keywordtype">int</span> i, j;
<a name="l00190"></a>00190                         __m128i r, r1, r2;
<a name="l00191"></a>00191                         <span class="keyword">static</span> <span class="keyword">const</span> __m128i mask = _mm_set_epi32(MSK4, MSK3, MSK2, MSK1);
<a name="l00192"></a>00192         
<a name="l00193"></a>00193                         r1 = _mm_load_si128(&amp;sfmt[N - 2].si);
<a name="l00194"></a>00194                         r2 = _mm_load_si128(&amp;sfmt[N - 1].si);
<a name="l00195"></a>00195                         <span class="keywordflow">for</span> (i = 0; i &lt; N - POS1; i++) {
<a name="l00196"></a>00196                                 r = mm_recursion(&amp;sfmt[i].si, &amp;sfmt[i + POS1].si, r1, r2, mask);
<a name="l00197"></a>00197                                 _mm_store_si128(&amp;array[i].si, r);
<a name="l00198"></a>00198                                 r1 = r2;
<a name="l00199"></a>00199                                 r2 = r;
<a name="l00200"></a>00200                         }
<a name="l00201"></a>00201                         <span class="keywordflow">for</span> (; i &lt; N; i++) {
<a name="l00202"></a>00202                                 r = mm_recursion(&amp;sfmt[i].si, &amp;array[i + POS1 - N].si, r1, r2, mask);
<a name="l00203"></a>00203                                 _mm_store_si128(&amp;array[i].si, r);
<a name="l00204"></a>00204                                 r1 = r2;
<a name="l00205"></a>00205                                 r2 = r;
<a name="l00206"></a>00206                         }
<a name="l00207"></a>00207                         <span class="comment">/* main loop */</span>
<a name="l00208"></a>00208                         <span class="keywordflow">for</span> (; i &lt; size - N; i++) {
<a name="l00209"></a>00209                                 r = mm_recursion(&amp;array[i - N].si, &amp;array[i + POS1 - N].si, r1, r2,
<a name="l00210"></a>00210                                         mask);
<a name="l00211"></a>00211                                 _mm_store_si128(&amp;array[i].si, r);
<a name="l00212"></a>00212                                 r1 = r2;
<a name="l00213"></a>00213                                 r2 = r;
<a name="l00214"></a>00214                         }
<a name="l00215"></a>00215                         <span class="keywordflow">for</span> (j = 0; j &lt; 2 * N - size; j++) {
<a name="l00216"></a>00216                                 r = _mm_load_si128(&amp;array[j + size - N].si);
<a name="l00217"></a>00217                                 _mm_store_si128(&amp;sfmt[j].si, r);
<a name="l00218"></a>00218                         }
<a name="l00219"></a>00219                         <span class="keywordflow">for</span> (; i &lt; size; i++) {
<a name="l00220"></a>00220                                 r = mm_recursion(&amp;array[i - N].si, &amp;array[i + POS1 - N].si, r1, r2,
<a name="l00221"></a>00221                                         mask);
<a name="l00222"></a>00222                                 _mm_store_si128(&amp;array[i].si, r);
<a name="l00223"></a>00223                                 _mm_store_si128(&amp;sfmt[j++].si, r);
<a name="l00224"></a>00224                                 r1 = r2;
<a name="l00225"></a>00225                                 r2 = r;
<a name="l00226"></a>00226                         }
<a name="l00227"></a>00227                 }
<a name="l00228"></a>00228                 <span class="keywordtype">void</span> init_gen_rand(uint32_t seed) {
<a name="l00229"></a>00229                         <span class="keywordtype">int</span> i;
<a name="l00230"></a>00230                         psfmt32[idxof(0)] = seed;
<a name="l00231"></a>00231                         <span class="keywordflow">for</span> (i = 1; i &lt; N32; i++) {
<a name="l00232"></a>00232                                 psfmt32[idxof(i)] = 1812433253UL * (psfmt32[idxof(i - 1)]
<a name="l00233"></a>00233                                 ^ (psfmt32[idxof(i - 1)] &gt;&gt; 30)) + i;
<a name="l00234"></a>00234                         }
<a name="l00235"></a>00235                         idx = N32;
<a name="l00236"></a>00236                         period_certification();
<a name="l00237"></a>00237                         initialized = 1;
<a name="l00238"></a>00238                 }
<a name="l00239"></a>00239                 <span class="keywordtype">void</span> init_by_array(uint32_t *init_key, <span class="keywordtype">int</span> key_length) {
<a name="l00240"></a>00240                         <span class="keywordtype">int</span> i, j, count;
<a name="l00241"></a>00241                         uint32_t r;
<a name="l00242"></a>00242                         <span class="keywordtype">int</span> lag;
<a name="l00243"></a>00243                         <span class="keywordtype">int</span> mid;
<a name="l00244"></a>00244                         <span class="keywordtype">int</span> size = N * 4;
<a name="l00245"></a>00245 
<a name="l00246"></a>00246                         <span class="keywordflow">if</span> (size &gt;= 623) {
<a name="l00247"></a>00247                                 lag = 11;
<a name="l00248"></a>00248                         } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (size &gt;= 68) {
<a name="l00249"></a>00249                                 lag = 7;
<a name="l00250"></a>00250                         } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (size &gt;= 39) {
<a name="l00251"></a>00251                                 lag = 5;
<a name="l00252"></a>00252                         } <span class="keywordflow">else</span> {
<a name="l00253"></a>00253                                 lag = 3;
<a name="l00254"></a>00254                         }
<a name="l00255"></a>00255                         mid = (size - lag) / 2;
<a name="l00256"></a>00256 
<a name="l00257"></a>00257                         memset(sfmt, 0x8b, <span class="keyword">sizeof</span>(sfmt));
<a name="l00258"></a>00258                         <span class="keywordflow">if</span> (key_length + 1 &gt; N32) {
<a name="l00259"></a>00259                                 count = key_length + 1;
<a name="l00260"></a>00260                         } <span class="keywordflow">else</span> {
<a name="l00261"></a>00261                                 count = N32;
<a name="l00262"></a>00262                         }
<a name="l00263"></a>00263                         r = func1(psfmt32[idxof(0)] ^ psfmt32[idxof(mid)] ^ psfmt32[idxof(N32 - 1)]);
<a name="l00264"></a>00264                         psfmt32[idxof(mid)] += r;
<a name="l00265"></a>00265                         r += key_length;
<a name="l00266"></a>00266                         psfmt32[idxof(mid + lag)] += r;
<a name="l00267"></a>00267                         psfmt32[idxof(0)] = r;
<a name="l00268"></a>00268 
<a name="l00269"></a>00269                         count--;
<a name="l00270"></a>00270                         <span class="keywordflow">for</span> (i = 1, j = 0; (j &lt; count) &amp;&amp; (j &lt; key_length); j++) {
<a name="l00271"></a>00271                                 r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]
<a name="l00272"></a>00272                                         ^ psfmt32[idxof((i + N32 - 1) % N32)]);
<a name="l00273"></a>00273                                 psfmt32[idxof((i + mid) % N32)] += r;
<a name="l00274"></a>00274                                 r += init_key[j] + i;
<a name="l00275"></a>00275                                 psfmt32[idxof((i + mid + lag) % N32)] += r;
<a name="l00276"></a>00276                                 psfmt32[idxof(i)] = r;
<a name="l00277"></a>00277                                 i = (i + 1) % N32;
<a name="l00278"></a>00278                         }
<a name="l00279"></a>00279                         <span class="keywordflow">for</span> (; j &lt; count; j++) {
<a name="l00280"></a>00280                                 r = func1(psfmt32[idxof(i)] ^ psfmt32[idxof((i + mid) % N32)]
<a name="l00281"></a>00281                                         ^ psfmt32[idxof((i + N32 - 1) % N32)]);
<a name="l00282"></a>00282                                 psfmt32[idxof((i + mid) % N32)] += r;
<a name="l00283"></a>00283                                 r += i;
<a name="l00284"></a>00284                                 psfmt32[idxof((i + mid + lag) % N32)] += r;
<a name="l00285"></a>00285                                 psfmt32[idxof(i)] = r;
<a name="l00286"></a>00286                                 i = (i + 1) % N32;
<a name="l00287"></a>00287                         }
<a name="l00288"></a>00288                         <span class="keywordflow">for</span> (j = 0; j &lt; N32; j++) {
<a name="l00289"></a>00289                                 r = func2(psfmt32[idxof(i)] + psfmt32[idxof((i + mid) % N32)]
<a name="l00290"></a>00290                                         + psfmt32[idxof((i + N32 - 1) % N32)]);
<a name="l00291"></a>00291                                 psfmt32[idxof((i + mid) % N32)] ^= r;
<a name="l00292"></a>00292                                 r -= i;
<a name="l00293"></a>00293                                 psfmt32[idxof((i + mid + lag) % N32)] ^= r;
<a name="l00294"></a>00294                                 psfmt32[idxof(i)] = r;
<a name="l00295"></a>00295                                 i = (i + 1) % N32;
<a name="l00296"></a>00296                         }
<a name="l00297"></a>00297                         idx = N32;
<a name="l00298"></a>00298                         period_certification();
<a name="l00299"></a>00299                         initialized = 1;
<a name="l00300"></a>00300                 }
<a name="l00301"></a>00301                 forceinline <span class="keywordtype">void</span> fill_array32(uint32_t *array, <span class="keywordtype">int</span> size) {
<a name="l00302"></a>00302                         assert(initialized);
<a name="l00303"></a>00303                         assert(idx == N32);
<a name="l00304"></a>00304                         assert(size % 4 == 0);
<a name="l00305"></a>00305                         assert(size &gt;= N32);
<a name="l00306"></a>00306                         gen_rand_array((w128_t *)array, size / 4);
<a name="l00307"></a>00307                         idx = N32;
<a name="l00308"></a>00308                 }
<a name="l00309"></a>00309                 forceinline <span class="keywordtype">void</span> fill_array64(uint64_t *array, <span class="keywordtype">int</span> size) {
<a name="l00310"></a>00310                         assert(initialized);
<a name="l00311"></a>00311                         assert(idx == N32);
<a name="l00312"></a>00312                         assert(size % 2 == 0);
<a name="l00313"></a>00313                         assert(size &gt;= N64);
<a name="l00314"></a>00314                         gen_rand_array((w128_t *)array, size / 2);
<a name="l00315"></a>00315                         idx = N32;
<a name="l00316"></a>00316                 }
<a name="l00317"></a>00317                 forceinline <span class="keyword">const</span> <span class="keywordtype">char</span> *get_idstring(<span class="keywordtype">void</span>)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> idstring; }
<a name="l00318"></a>00318                 forceinline <span class="keywordtype">int</span> get_min_array_size32(<span class="keywordtype">void</span>)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> N32; }
<a name="l00319"></a>00319                 forceinline <span class="keywordtype">int</span> get_min_array_size64(<span class="keywordtype">void</span>)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> N64; }
<a name="l00320"></a>00320                 forceinline <span class="keywordtype">double</span> genrand_real3(<span class="keywordtype">void</span>) { <span class="keywordflow">return</span> to_real3(gen_rand32()); }
<a name="l00321"></a>00321                 forceinline <span class="keywordtype">double</span> genrand_real2(<span class="keywordtype">void</span>) { <span class="keywordflow">return</span> to_real2(gen_rand32()); }
<a name="l00322"></a>00322                 forceinline <span class="keywordtype">double</span> genrand_real1(<span class="keywordtype">void</span>) { <span class="keywordflow">return</span> to_real1(gen_rand32()); }
<a name="l00323"></a>00323                 forceinline <span class="keywordtype">double</span> genrand_res53(<span class="keywordtype">void</span>) { <span class="keywordflow">return</span> to_res53(gen_rand64()); }
<a name="l00324"></a>00324                 forceinline <span class="keywordtype">double</span> genrand_res53_mix(<span class="keywordtype">void</span>) { 
<a name="l00325"></a>00325                         uint32_t x, y;
<a name="l00326"></a>00326                         x = gen_rand32();
<a name="l00327"></a>00327                         y = gen_rand32();
<a name="l00328"></a>00328                         <span class="keywordflow">return</span> to_res53_mix(x, y);
<a name="l00329"></a>00329                 }
<a name="l00330"></a>00330                 uint32_t gen_rand32(<span class="keywordtype">void</span>) {
<a name="l00331"></a>00331                         uint32_t r;
<a name="l00332"></a>00332 
<a name="l00333"></a>00333                         assert(initialized);
<a name="l00334"></a>00334                         <span class="keywordflow">if</span> (idx &gt;= N32) {
<a name="l00335"></a>00335                                 gen_rand_all();
<a name="l00336"></a>00336                                 idx = 0;
<a name="l00337"></a>00337                         }
<a name="l00338"></a>00338                         r = psfmt32[idx++];
<a name="l00339"></a>00339                         <span class="keywordflow">return</span> r;
<a name="l00340"></a>00340                 }
<a name="l00341"></a>00341                 uint64_t gen_rand64(<span class="keywordtype">void</span>) {
<a name="l00342"></a>00342                         uint64_t r;
<a name="l00343"></a>00343                         assert(initialized);
<a name="l00344"></a>00344                         assert(idx % 2 == 0);
<a name="l00345"></a>00345 
<a name="l00346"></a>00346                         <span class="keywordflow">if</span> (idx &gt;= N32) {
<a name="l00347"></a>00347                                 gen_rand_all();
<a name="l00348"></a>00348                                 idx = 0;
<a name="l00349"></a>00349                         }
<a name="l00350"></a>00350                         r = psfmt64[idx / 2];
<a name="l00351"></a>00351                         idx += 2;
<a name="l00352"></a>00352                         <span class="keywordflow">return</span> r;
<a name="l00353"></a>00353                 }
<a name="l00354"></a>00354                 <span class="comment">//return 128-bit xmm register</span>
<a name="l00355"></a>00355                 __m128i gen_randx128(<span class="keywordtype">void</span>) {
<a name="l00356"></a>00356                         __m128i r;
<a name="l00357"></a>00357                         assert(initialized);
<a name="l00358"></a>00358                         assert(idx % 2 == 0);
<a name="l00359"></a>00359 
<a name="l00360"></a>00360                         <span class="keywordflow">if</span> (idx &gt;= N32) {
<a name="l00361"></a>00361                                 gen_rand_all();
<a name="l00362"></a>00362                                 idx = 0;
<a name="l00363"></a>00363                         }
<a name="l00364"></a>00364                         r = sfmt[idx / 4].si;
<a name="l00365"></a>00365                         idx += 4;
<a name="l00366"></a>00366                         <span class="keywordflow">return</span> r;
<a name="l00367"></a>00367                 }
<a name="l00368"></a>00368         };
<a name="l00369"></a>00369 
<a name="l00370"></a>00370         <span class="keyword">typedef</span> sse2_fast_mersenne_twister&lt;607, 2, 15, 3, 13, 3,
<a name="l00371"></a>00371                 0xfdff37ffU, 0xef7f3f7dU, 0xff777b7dU, 0x7ff7fb2fU,
<a name="l00372"></a>00372                 0x00000001U, 0x00000000U, 0x00000000U, 0x5986f054U&gt; sfmt607;
<a name="l00373"></a>00373 
<a name="l00374"></a>00374         <span class="keyword">typedef</span> sse2_fast_mersenne_twister&lt;1279, 7, 14, 3, 5, 1,
<a name="l00375"></a>00375                 0xf7fefffdU, 0x7fefcfffU, 0xaff3ef3fU, 0xb5ffff7fU,
<a name="l00376"></a>00376                 0x00000001U, 0x00000000U, 0x00000000U, 0x20000000U&gt; sfmt1279;
<a name="l00377"></a>00377 
<a name="l00378"></a>00378         <span class="keyword">typedef</span> sse2_fast_mersenne_twister&lt;2281, 12, 19, 1, 5, 1,
<a name="l00379"></a>00379                 0xbff7ffbfU, 0xfdfffffeU, 0xf7ffef7fU, 0xf2f7cbbfU,
<a name="l00380"></a>00380                 0x00000001U, 0x00000000U, 0x00000000U, 0x41dfa600U&gt; sfmt2281;
<a name="l00381"></a>00381 
<a name="l00382"></a>00382         <span class="keyword">typedef</span> sse2_fast_mersenne_twister&lt;4253, 17, 20, 1, 7, 1,
<a name="l00383"></a>00383                 0x9f7bffff, 0x9fffff5f, 0x3efffffb, 0xfffff7bb,
<a name="l00384"></a>00384                 0xa8000001U, 0xaf5390a3U, 0xb740b3f8U, 0x6c11486dU&gt; sfmt4253;
<a name="l00385"></a>00385 
<a name="l00386"></a>00386         <span class="keyword">typedef</span> sse2_fast_mersenne_twister&lt;12213, 68, 14, 3, 7, 3, 
<a name="l00387"></a>00387                 0xeffff7fbU, 0xffffffefU, 0xdfdfbfffU, 0x7fffdbfdU,
<a name="l00388"></a>00388                 0x00000001U, 0x00000000U, 0xe8148000U, 0xd0c7afa3U&gt; sfmt12213;
<a name="l00389"></a>00389 
<a name="l00390"></a>00390         <span class="keyword">typedef</span> sse2_fast_mersenne_twister&lt;19937, 122, 18, 1, 11, 1, 
<a name="l00391"></a>00391                 0xdfffffefU, 0xddfecb7fU, 0xbffaffffU, 0xbffffff6U,
<a name="l00392"></a>00392                 0x00000001U, 0x00000000U, 0x00000000U, 0x13c9e684U&gt; sfmt19937;
<a name="l00393"></a>00393 
<a name="l00394"></a>00394         <span class="keyword">typedef</span> sse2_fast_mersenne_twister&lt;44497, 330, 5, 3, 9, 3,
<a name="l00395"></a>00395                 0xeffffffbU, 0xdfbebfffU, 0xbfbf7befU, 0x9ffd7bffU,
<a name="l00396"></a>00396                 0x00000001U, 0x00000000U, 0xa3ac4000U, 0xecc1327aU&gt; sfmt44497;
<a name="l00397"></a>00397 
<a name="l00398"></a>00398         <span class="keyword">typedef</span> sse2_fast_mersenne_twister&lt;86243, 366, 6, 7,19, 1,
<a name="l00399"></a>00399                 0xfdbffbffU, 0xbff7ff3fU, 0xfd77efffU, 0xbf9ff3ffU, 
<a name="l00400"></a>00400                 0x00000001U, 0x00000000U, 0x00000000U, 0xe9528d85U&gt; sfmt86243;
<a name="l00401"></a>00401 
<a name="l00402"></a>00402         <span class="keyword">typedef</span> sse2_fast_mersenne_twister&lt;132049, 110, 19, 1, 21, 1, 
<a name="l00403"></a>00403                 0xffffbb5fU, 0xfb6ebf95U, 0xfffefffaU, 0xcff77fffU,
<a name="l00404"></a>00404                 0x00000001U, 0x00000000U, 0xcb520000U, 0xc7e91c7dU&gt; sfmt132049; 
<a name="l00405"></a>00405 
<a name="l00406"></a>00406         <span class="keyword">typedef</span> sse2_fast_mersenne_twister&lt;216091, 627, 11, 3, 10, 1,
<a name="l00407"></a>00407                 0xbff7bff7U, 0xbfffffffU, 0xbffffa7fU, 0xffddfbfbU, 
<a name="l00408"></a>00408                 0xf8000001U, 0x89e80709U, 0x3bd2b64bU, 0x0c64b1e4U&gt; sfmt216091;
<a name="l00409"></a>00409 
<a name="l00410"></a>00410 };<span class="comment">//sfmtplus</span>
<a name="l00411"></a>00411 
<a name="l00412"></a>00412 <span class="preprocessor">#endif //SSE2_SFMT_PLUS__</span>
</pre></div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><b>sfmtrand.h</b>      </li>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>


    <li class="footer">Generated on Thu Dec 15 2011 11:56:20 for nih by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
